# -*- coding: utf-8 -*-
"""
@Auth ： Wangchongzhi
@File ：fall_down_detection.py
"""
from tf_pose.estimator import TfPoseEstimator
from utils import img_util

algo_id = 50


def queue_img_get(result_img_q, stream_q, device):
    fall_dict = {}
    i = 0
    while True:
        if result_img_q.qsize() > 0:
            image_info = result_img_q.get()
            if isinstance(image_info, tuple):
                image, humans, time1 = image_info
                save = False
                image, fall = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
                if fall:
                    save = True
                    fall_dict[i] = []
                    i += 1
                for j in range(6):
                    try:
                        fall_dict[j] += [fall]
                        if sum(fall_dict[j]) == 3:
                            del fall_dict[j]
                        if len(fall_dict[j]) == 6:
                            del fall_dict[j]
                    except:
                        continue
                stream_q.put(image)
                if save:
                    img_util.save_image(image, device, algo_id)
            else:
                stream_q.put(image_info)
            if i % 6 == 0:
                # logger.info('fall_down++++'*8)
                i = 0



